package com.szewec.ps.ui.project;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.szewec.ps.R;
import com.szewec.ps.base.BaseActivity;
import com.szewec.ps.base.Enums;
import com.szewec.ps.databinding.ActivityProjectTypeBinding;
import com.szewec.ps.myview.toast.bamtoast.BamToast;
import com.szewec.ps.ui.member.MemberSelectActivity;
import com.szewec.ps.util.OneOffCupboard;

import org.jetbrains.annotations.NotNull;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;

public class ProjectTypeActivity extends BaseActivity {
    public static final String KEY_SELECTED_PROJTYPE_GRADE1 = "KEY_SELECTED_PROJTYPE_GRADE1";
    public static final String KEY_SELECTED_PROJTYPE_GRADE2 = "KEY_SELECTED_PROJTYPE_GRADE2";

    private ActivityProjectTypeBinding _binding;

    private ProjTypeGrade2Adapter _typeGrade2Adapter;
    List<Enums.ProjTypeGrade2> _typeGrade2List = new ArrayList<>();

    private ProjTypeGrade1Adapter _typeGrade1Adapter;
    List<Enums.ProjTypeGrade1> _typeGrade1List = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _binding = DataBindingUtil.setContentView(this, R.layout.activity_project_type);
        ButterKnife.bind(this);


        _typeGrade1List = Arrays.asList(Enums.ProjTypeGrade1.values());
        LinearLayoutManager manager0 = new LinearLayoutManager(this);
        manager0.setOrientation(LinearLayoutManager.VERTICAL);
        _binding.leftRecyclerView.setLayoutManager(manager0);
        _typeGrade1Adapter = new ProjTypeGrade1Adapter(this);
        _binding.leftRecyclerView.setAdapter(_typeGrade1Adapter);


        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        _binding.rightRecyclerView.setLayoutManager(manager);
        _typeGrade2Adapter = new ProjTypeGrade2Adapter(this);
        _binding.rightRecyclerView.setAdapter(_typeGrade2Adapter);


        initTitleBar();
    }

    private void initTitleBar(){
        //显示返回按钮
        _binding.titleBar.setLeftLayoutVisibility(View.VISIBLE);
        _binding.titleBar.setLeftLayoutClickListener(this::finish);
        //设置右边按钮
        _binding.titleBar.setRightTextVisibility(View.VISIBLE);
        _binding.titleBar.setRightText("确定");
        _binding.titleBar.setRightLayoutClickListener(()->{
            if (_typeGrade2Adapter._selectedProjTypeGrade2 == null || _typeGrade1Adapter._selectedProjTypeGrade1 == null){
                BamToast.showText(ProjectTypeActivity.this, "您还没有选择类型！");
                return;
            }

            OneOffCupboard.sharedInstance().set(KEY_SELECTED_PROJTYPE_GRADE1, _typeGrade1Adapter._selectedProjTypeGrade1);
            OneOffCupboard.sharedInstance().set(KEY_SELECTED_PROJTYPE_GRADE2, _typeGrade2Adapter._selectedProjTypeGrade2);

            setResult(RESULT_OK, new Intent());
            finish();
        });
    }

    //一级类型
    public class ProjTypeGrade1Adapter extends RecyclerView.Adapter<ProjTypeGrade1Adapter.ProjTypeGrade1ViewHolder>{

        //内部类，绑定控件
        class ProjTypeGrade1ViewHolder extends RecyclerView.ViewHolder{
            TextView titleTV;
            View selectedView;
            public ProjTypeGrade1ViewHolder(View itemView) {
                super(itemView);
                titleTV = itemView.findViewById(R.id.proj_Type_Grade1);
                selectedView = itemView.findViewById(R.id.ic_Selected);
            }
        }

        private Context context;

        Enums.ProjTypeGrade1 _selectedProjTypeGrade1;
        private WeakReference<ProjTypeGrade1ViewHolder> nowUseViewHolder = null;

        public ProjTypeGrade1Adapter(Context context){
            this.context = context;
        }

        //getItemViewType返回行号的化，onCreateViewHolder的viewType就是行号
        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @NotNull
        @Override
        public ProjTypeGrade1ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            //获取添加视图
            View contentView = LayoutInflater.from(context).inflate(R.layout.item_proj_type_grade1_cell, parent, false);
            return new ProjTypeGrade1ViewHolder(contentView);
        }


        @Override
        public void onBindViewHolder(@NotNull ProjectTypeActivity.ProjTypeGrade1Adapter.ProjTypeGrade1ViewHolder viewHolder, int position) {
            Enums.ProjTypeGrade1 projTypeGrade1 = _typeGrade1List.get(position);
            viewHolder.titleTV.setText(projTypeGrade1.getValue());
            setUnselectStyle(viewHolder);

            viewHolder.itemView.setOnClickListener((view)->{
                if (nowUseViewHolder != null && nowUseViewHolder.get() == viewHolder){
                    return;
                }
                if (nowUseViewHolder != null){
                    setUnselectStyle(nowUseViewHolder.get());
                }
                nowUseViewHolder = new WeakReference<>(viewHolder);
                setSelectedStyle(nowUseViewHolder.get());

                _selectedProjTypeGrade1 = projTypeGrade1;

                //设置二级类型
                _typeGrade2List = Enums.ProjTypeGrade2.getEnumListByParentKey(projTypeGrade1.getKey());
                _typeGrade2Adapter.notifyDataSetChanged();
            });
        }

        private void setUnselectStyle(ProjTypeGrade1ViewHolder viewHolder){
            viewHolder.titleTV.setTextColor(getResources().getColor(R.color.C333333));
            viewHolder.selectedView.setVisibility(View.GONE);
        }

        private void setSelectedStyle(ProjTypeGrade1ViewHolder viewHolder){
            viewHolder.titleTV.setTextColor(getResources().getColor(R.color.C2A84FC));
            viewHolder.selectedView.setVisibility(View.VISIBLE);
        }

        @Override
        public int getItemCount() {
            return _typeGrade1List.size();
        }
    }

    //二级类型
    public class ProjTypeGrade2Adapter extends RecyclerView.Adapter<ProjTypeGrade2Adapter.ProjTypeGrade2ViewHolder>{

        private Context context;

        Enums.ProjTypeGrade2 _selectedProjTypeGrade2;
        private WeakReference<ProjTypeGrade2ViewHolder> nowUseViewHolder = null;

        public ProjTypeGrade2Adapter(Context context){
            this.context = context;
        }

        //getItemViewType返回行号的化，onCreateViewHolder的viewType就是行号
        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @NotNull
        @Override
        public ProjectTypeActivity.ProjTypeGrade2Adapter.ProjTypeGrade2ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            //获取添加视图
            View contentView = LayoutInflater.from(context).inflate(R.layout.item_proj_type_grade2_cell, parent, false);
            return new ProjTypeGrade2ViewHolder(contentView);
        }


        @Override
        public void onBindViewHolder(@NotNull ProjectTypeActivity.ProjTypeGrade2Adapter.ProjTypeGrade2ViewHolder viewHolder, int position) {
            Enums.ProjTypeGrade2 projTypeGrade2 = _typeGrade2List.get(position);
            viewHolder.leftTV.setText(projTypeGrade2.getValue());
            setUnselectStyle(viewHolder);

            viewHolder.itemView.setOnClickListener((view)->{
                if (nowUseViewHolder != null && nowUseViewHolder.get() == viewHolder){
                    return;
                }
                if (nowUseViewHolder != null){
                    setUnselectStyle(nowUseViewHolder.get());
                }
                nowUseViewHolder = new WeakReference<>(viewHolder);
                setSelectedStyle(nowUseViewHolder.get());

                _selectedProjTypeGrade2 = projTypeGrade2;
            });
        }

        private void setUnselectStyle(ProjTypeGrade2ViewHolder viewHolder){
            viewHolder.leftTV.setTextColor(getResources().getColor(R.color.C333333));
            viewHolder.selectedIcon.setVisibility(View.GONE);
        }

        private void setSelectedStyle(ProjTypeGrade2ViewHolder viewHolder){
            viewHolder.leftTV.setTextColor(getResources().getColor(R.color.C2A84FC));
            viewHolder.selectedIcon.setVisibility(View.VISIBLE);
        }

        @Override
        public int getItemCount() {
            return _typeGrade2List.size();
        }

        //内部类，绑定控件
        class ProjTypeGrade2ViewHolder extends RecyclerView.ViewHolder{
            TextView leftTV;
            ImageView selectedIcon;
            public ProjTypeGrade2ViewHolder(View itemView) {
                super(itemView);
                leftTV = itemView.findViewById(R.id.member_Name);
                selectedIcon = itemView.findViewById(R.id.ic_selected);
            }
        }
    }
}
